﻿MainClass = function () {
    var self = this;

    function Global() {
        this.Identity = 1;
        this.NewID = function () {
            return this.Identity++;
        };
    }
    var G = new Global();

    Ext.selection.TreeModel.prototype.onKeyUp = Ext.Function.createInterceptor(Ext.selection.TreeModel.prototype.onKeyUp, function () {
        var store = this.store,
            idx = store.indexOf(this.lastFocused),
            visIdx,
            record,
            focusSearchField;

        if (idx === 0) {
            focusSearchField = true;
        } else {
            record = store.getAt(idx - 1);
            visIdx = idx - 1;

            while (visIdx > 0 && record.data.hidden) {
                record = store.getAt(--visIdx);
            }

            if (record.data.hidden) {
                focusSearchField = true;
            }
        }

        if (focusSearchField) {
            this.deselectAll();
            App.SearchField.focus(false, 100);
        }
    });


    var makeTab = function (id, url, title) {
        var tabTip;

        if (id === "-") {
            id = Ext.id(undefined, "extnet");
            lookup[url] = id;
        }

        tabTip = url.replace(/^\//g, "");
        tabTip = tabTip.replace(/\/$/g, "");
        tabTip = tabTip.replace(/\//g, " > ");
        tabTip = tabTip.replace(/_/g, " ");

        var testTab;
        Ext.each(App.ExampleTabs.items.items,
            function (item, index) {
                if (tabTip === item.tabTip) {
                    testTab = item;
                    return;
                }
            });

        if (testTab != null) {
            App.ExampleTabs.setActiveTab(testTab);

            return;
        }

        var hostName = window.location.protocol + "//" + window.location.host;

        var newId = G.NewID();
        var tab = App.ExampleTabs.add(Ext.create('Ext.panel.Panel',{
            id: "ExampleTabs_Panel" + newId,
            title: title,
            tabTip: tabTip,
            layout: "fit",
            hideMode: "offsets",
            loader: {
                scripts: true,
                //autoLoad: true,
                renderer: function (loader, response, active) {
                    loader.getTarget().update(response.responseText, true);
                    return true;
                },
                loadMask: true,
                url: hostName + url,
                params: { "containerId": "ExampleTabs_Panel" + newId },
                //failure: function () { alert("加载错误"); },
                //success: function () { alert("加载success"); }
                listeners: {
                    exception: function (load, response, options, eOpts) {
                        Ext.MessageBox.alert("提示", "页面加载出现异常,请联系管理员");
                    },
                    beforeload: function (load, options, eOpts) {
                        load.target.body.mask('正在加载...');
                    },
                    load: function (load, response, options, eOpts) {
                        if (load.target.body)
                            load.target.body.unmask();
                    }
                }
            },
            closable: true
        }));

        setTimeout(function () {
            App.ExampleTabs.setActiveTab(tab);
        }, 250);


    };

    var lookup = {};

    self.onTreeItemClick = function (record, e) {
        if (record.isLeaf()) {
            e.stopEvent();
            self.loadExample(record.get('route'), record.getId(), record.get('text'));
        } else {
            record[record.isExpanded() ? 'collapse' : 'expand']();
        }
    };


    self.loadExample = function (href, id, title) {
        var tab = App.ExampleTabs.getComponent(id);
        if (id === "-") {
            App.direct.GetHashCode(href, {
                success: function (result) {
                    self.loadExample(href, "e" + result, title);
                }
            });

            return;
        }

        lookup[href] = id;

        if (tab) {
            App.ExampleTabs.setActiveTab(tab);
        } else {
            if (Ext.isEmpty(title)) {
                var m = /(\w+)\/$/g.exec(href);
                title = m == null ? "[No name]" : m[1];
            }

            title = title.replace(/<span>&nbsp;<\/span>/g, "");
            title = title.replace(/_/g, " ");
            makeTab(id, href, title);
        }
    };


    
    
}
